package main

import (
	"fmt"
	"os"
	codecs "sanguo/codec/cs"
	"sanguo/node/client/dispatcher"
	"sanguo/node/client/login"
	cs_msg "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage selectRole userID addr\n")
		return
	}

	userID := os.Args[1]
	addr := os.Args[2]

	login.Login(userID, addr, func(session kendynet.StreamSession, d *dispatcher.Dispatcher, msg *codecs.Message, err error) {
		if nil != err {
			fmt.Println(err)
		} else {
			gameLoginToC := msg.GetData().(*cs_msg.GameLoginToC)
			fmt.Println("RoleList", gameLoginToC)
			if gameLoginToC.GetCode() == cs_msg.EnumType_OK {
				var roleIDMap map[int]uint64 = make(map[int]uint64)

				req := &cs_msg.RoleListToS{}
				session.Send(codecs.NewMessage(uint16(0), req))

				d.Register(&cs_msg.RoleBattleAttrToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleBattleAttrToC)
					fmt.Println("RoleBattleAttrToC", resp)

				})
				d.Register(&cs_msg.GetWeaponCollectionAwardToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.GetWeaponCollectionAwardToC)
					fmt.Println("GetWeaponCollectionAwardToC", resp)

				})
				d.Register(&cs_msg.WeaponActivateToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.WeaponActivateToC)
					fmt.Println("WeaponActivateToC", resp)

				})
				d.Register(&cs_msg.WeaponUpStarToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.WeaponUpStarToC)
					fmt.Println("WeaponUpStarToC", resp)

				})
				d.Register(&cs_msg.WeaponStarMapUpLevelToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.WeaponStarMapUpLevelToC)
					fmt.Println("WeaponStarMapUpLevelToC", resp)

				})

				d.Register(&cs_msg.ChangeOutlineToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.ChangeOutlineToC)
					fmt.Println("ChangeOutlineToC", resp)

				})
				d.Register(&cs_msg.RoleListToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleListToC)
					fmt.Println(resp.GetErrcode())
					for k, v := range resp.RoleList {
						fmt.Println(k, v)
						roleIDMap[k] = v.GetID()
						fmt.Println(k, roleIDMap[k])
					}

					if resp.GetErrcode() == cs_msg.EnumType_OK {
						req2 := &cs_msg.SelectRoleToS{
							ID: proto.Uint64(roleIDMap[0]),
						}
						session.Send(codecs.NewMessage(uint16(0), req2))
						d.Register(&cs_msg.SelectRoleToC{}, false, func(args []interface{}) {
							resp2 := args[1].(*codecs.Message).GetData().(*cs_msg.SelectRoleToC)
							fmt.Println("SelectRoleToC", resp2)
							if resp2.GetErrcode() == cs_msg.EnumType_OK {
								//for {
								fmt.Printf("1武器激活 2武器升星 3星图升级 4领取星图收集奖励：")
								var k int
								fmt.Scan(&k)
								if k == 1 {
									ActivateWeapon(session)
								}
								if k == 2 {
									UpStarWeapon(session, resp2)
								}
								if k == 3 {
									StarMapUpLev(session)
								}
								if k == 4 {
									GetStarMap(session, resp2)
								}
								//}
							}
						})
					}

				})

			}
		}
	})

	sigStop := make(chan bool)
	_, _ = <-sigStop
}

func ActivateWeapon(session kendynet.StreamSession) {
	go func() {
		fmt.Printf("装备ID:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.WeaponActivateToS{
			WeaponId: proto.Int32(int32(id)),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func UpStarWeapon(session kendynet.StreamSession, resp *cs_msg.SelectRoleToC) {
	go func() {
		for _, v := range resp.GetWeapons() {
			fmt.Println("weaponId:", v.GetWeaponId(), v.GetState())
		}
		fmt.Printf("武器ID:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.WeaponUpStarToS{
			WeaponId: proto.Int32(int32(id)),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func StarMapUpLev(session kendynet.StreamSession) {
	go func() {
		req := &cs_msg.WeaponStarMapUpLevelToS{}
		session.Send(codecs.NewMessage(uint16(0), req))

	}()
}

func GetStarMap(session kendynet.StreamSession, resp *cs_msg.SelectRoleToC) {
	go func() {
		for _, v := range resp.GetStarMap().GetWeaponCollections() {
			fmt.Println("collection:", v.GetID(), v.GetProgress())
		}
		fmt.Printf("ID:")
		var id int
		fmt.Scan(&id)
		req := &cs_msg.GetWeaponCollectionAwardToS{
			CollectionId: proto.Int32(int32(id)),
		}
		session.Send(codecs.NewMessage(uint16(0), req))

	}()
}
